import 'index.dart';
import 'package:flutter/material.dart';

class FilterRadioPage extends StatefulWidget with FilterPopupBaseWidget {
  static final GlobalKey<_FilterRadioState> _globalKey =
      GlobalKey<_FilterRadioState>();

  Offset offsetLT;
  FilterItemData itemData;
  SelectCallback onSelect;
  DismissCallback onDismiss;

  FilterRadioPage(
      {required this.itemData,
      required this.onSelect,
      required this.onDismiss,
      this.offsetLT = Offset.zero})
      : super(key: _globalKey);

  @override
  _FilterRadioState createState() => _FilterRadioState();

  @override
  dismiss() {
    _FilterRadioState? state = _globalKey.currentState;
    state?.dismiss();
  }
}

class _FilterRadioState extends State<FilterRadioPage>
    with SingleTickerProviderStateMixin {
  late FilterItemData _itemData;

  late Animation<Offset> _animation;
  late AnimationController _controller;

  @override
  void initState() {
    super.initState();
    //对象深拷贝
    _itemData = FilterItemData.fromJson(widget.itemData.toJson());
    _controller = AnimationController(
      vsync: this,
      duration: Duration(milliseconds: 200),
    );
    _animation = Tween<Offset>(begin: const Offset(0, -1), end: Offset.zero)
        .animate(_controller);
    _controller.forward();
  }

  dismiss() {
    widget.onDismiss();
    _controller.reverse();
  }

  @override
  void dispose() {
    super.dispose();
    _controller.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.transparent,
      body: Container(
        padding: EdgeInsets.only(top: widget.offsetLT.dy),
        child: ClipRect(
          child: SlideTransition(
            position: _animation,
            child: GestureDetector(
              onTap: () {
                //阻断内容部分点击关闭效果
              },
              child: Container(
                constraints: BoxConstraints(maxHeight: 400),
                color: Theme.of(context).colorScheme.background,
                child: ListView.builder(
                  padding: EdgeInsets.zero,
                  shrinkWrap: true,
                  itemCount: _itemData.children!.length,
                  itemExtent: 50,
                  itemBuilder: (context, index) {
                    FilterItemData itemData = _itemData.children![index];
                    return ListTile(
                      title: Text(itemData.name),
                      trailing: itemData.isSelect()
                          ? Icon(
                              Icons.check_box,
                              color: Theme.of(context).primaryColor,
                            )
                          : const Icon(Icons.check_box_outline_blank),
                      onTap: () {
                        setState(() {
                          for (FilterItemData mItemData
                              in _itemData.children!) {
                            if (mItemData != itemData) {
                              if (itemData.code == mItemData.code) {
                                mItemData.setSelect(false);
                              }
                            } else {
                              itemData.setSelect(true);
                            }
                          }
                        });
                        widget.onSelect(_itemData);

                        filterPop(context);
                      },
                    );
                  },
                ),
              ),
            ),
          ),
        ),
      ),
    );
  }
}
